package com.woniuxy.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nimbusds.jose.JOSEException;
import com.woniuxy.entity.DTO.BackLoginDTO;
import com.woniuxy.entity.DTO.UpdateAdministratorPasswordDTO;
import com.woniuxy.entity.DTO.UserLoginDTO;
import com.woniuxy.entity.DTO.UserRegisterDTO;
import com.woniuxy.entity.Goods;
import com.woniuxy.entity.User;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.service.UserService;
import com.woniuxy.util.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageInfo queryUser(PageParam<User> pageParam) {
        //参数1：pageNum(当前页)  参数2：pageSize(每页多少条参数)
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<User> users = userMapper.queryUser(pageParam.getParam());
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return userPageInfo;

    }

    @Override
    public ResponseData register(UserRegisterDTO userRegisterDTO) {
        Boolean b = userMapper.queryUserByAccount(userRegisterDTO.getUserAccount());
        if(b){
            return ResponseData.fail(ResponseEnum.ACCOUNT_EXISTS_FAIL);
        }
        String password = SecureUtil.md5(userRegisterDTO.getUserPassword() + "1a2b3c");
        userRegisterDTO.setUserPassword(password);

         userMapper.register(userRegisterDTO);

         return ResponseData.success();
    }

    @Override
    public ResponseData login(UserLoginDTO userLoginDTO) throws JOSEException {
        UserLoginDTO userInfo = userMapper.queryUserPasswordByAccount(userLoginDTO.getUserAccount());
        if (userInfo == null) {
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        } else {
            String md5Password = SecureUtil.md5(userLoginDTO.getUserPassword() + "1a2b3c");
            if (userInfo.getUserPassword().equals(md5Password)) {
                HashMap<String, Object> userMap = new HashMap<>();
                userMap.put("id",userInfo.getId());
                userMap.put("account",userInfo.getUserAccount());
                String jwt = JWTUtil.createJWT(userMap);
                stringRedisTemplate.opsForValue().set("JWT:"+userInfo.getId(),jwt,60000, TimeUnit.SECONDS);

                return ResponseData.success(jwt);
            } else {
                return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
            }
        }

    }

    @Override
    public ResponseData queryUserInfo() throws ParseException {
        Integer userId = UserContext.getUserId();
        return ResponseData.success(userMapper.queryUserInfo(userId));
    }

    @Override
    public void imgLoad(Integer id, String imgSrc) {
        userMapper.imgLoad(id,imgSrc);
    }

    @Override
    public ResponseData Backlogin(BackLoginDTO backLoginDTO) {
        BackLoginDTO backlogin = userMapper.Backlogin(backLoginDTO);
        if(backlogin == null){
            return ResponseData.fail(ResponseEnum.LOGIN_FAIL);
        }
        return ResponseData.success(backlogin);
    }

    @Override
    public ResponseData updateAdministrator(BackLoginDTO backLoginDTO) {
        userMapper.updateAdministrator(backLoginDTO);
        return ResponseData.success();
    }

    @Override
    public ResponseData queryAdministratorInfo(Integer administratorId) {
        BackLoginDTO backLoginDTO = userMapper.queryAdministratorInfo(administratorId);
        return ResponseData.success(backLoginDTO);
    }

    @Override
    public void imgSrcLoad(Integer id, String imgSrc) {
        userMapper.imgSrcLoad(id,imgSrc);
    }

    @Override
    public void updateAdministratorPassword(UpdateAdministratorPasswordDTO updateAdministratorPasswordDTO) {

        userMapper.updateAdministratorPassword(updateAdministratorPasswordDTO);

    }

    @Override
    public void updateLoginTime() throws ParseException {
        Integer userId = UserContext.getUserId();
        userMapper.updateLoginTime(userId);
    }
}


